package com.hqyj.lyf.service.impl;

import com.hqyj.lyf.pojo.QuestionBank;
import com.hqyj.lyf.pojo.Subject;
import com.hqyj.lyf.service.QuestionBankService;
import com.hqyj.lyf.service.SubjectService;
import com.hqyj.lyf.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Qiuluoer
 * @date 2022/6/1 20:27
 */
@Service
public class FileUploadService {

    @Autowired
    private QuestionBankService questionBankService;

    public Map addQuestions (MultipartFile file) {
        Map<String, Object> map = new HashMap<>();

        String name=file.getOriginalFilename();
        System.out.println(name);

        //上传路径保存设置
        String path = "D:\\j2202springboot_template\\file";

        File realPath = new File(path);

        if (!realPath.exists()){
            realPath.mkdir();
        }
        //上传文件地址
        System.out.println("上传文件保存地址："+realPath);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）

        if(name.substring(name.length()-4).equals(".xls")){

            try {
                map = questionBankService.addQuestions(file);

            } catch (IOException e) {
                e.printStackTrace();
                map.put("code","-1");
                map.put("msg","批量插入失败");
            }

        }else {
            try {
                file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));
                map.put("code",0);
                map.put("msg","上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code",-2);
                map.put("msg","上传失败");
            }
        }

        return map;
    }
}
